﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.SqlServer.Management.Smo;
using Microsoft.SqlServer.Management.Smo.Agent;
using Microsoft.SqlServer.Management.Common;
using System.Data.SqlClient;
using System.Security.Cryptography;
using System.Text.RegularExpressions;

namespace TestSmo
{
    class Program
    {
        static void Main(string[] args)
        {
            //string connection_string = @"Data Source=KLDB3\SQL2008;Initial Catalog=CDMS_UAT;Persist Security Info=True;User ID=CDMS_UAT_user;Password=Xh57887266";
            
            // SqlConnection sql_connection = new SqlConnection(connection_string);

            // Program to generate random password that meets guidelines
            // Guideline dictates:
            // Minimum length:      8 characters
            // Contains             1 alpha (upper)
            // Contains             1 alpha (lower)
            // Contains             1 numeric
            // from character set "1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"

            Console.WriteLine("[START]");

            int targetLength = 8;
            char[] charSet = "1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray();

            bool hasDigit = false;
            bool hasLowerCaseLetter = false;
            bool hasUpperCaseLetter = false;
            bool acceptPassword = false;

            int idx;
            char[] newPasswordChar;
            string newPassword;

            for (int x = 0; x < 50; x++)
            {
                acceptPassword = false;
                hasDigit = false;
                hasLowerCaseLetter = false;
                hasUpperCaseLetter = false;
                newPasswordChar = new char[targetLength];

                while (!acceptPassword)
                {
                    using (RandomNumberGenerator rng = RNGCryptoServiceProvider.Create())
                    {
                        // Create a byte array to hold the random value. 
                        byte[] randomNumber = new byte[targetLength];

                        rng.GetBytes(randomNumber);

                        for (int i = 0; i < targetLength; i++)
                        {
                            idx = (int)Math.Truncate(((double)randomNumber[i] / byte.MaxValue) * 61);
                            newPasswordChar[i] = charSet[idx];
                        }

                        newPassword = new String(newPasswordChar);

                        // Check if new password is valid 
                        // new Regex("^[A-Za-z0-9]+$) <= Not completely correct!
                        // What we want to say is that newPassword should have 1 UCASE, 1 LCASE, and 1 DIGIT minimal

                        hasDigit = Regex.IsMatch(newPassword, "[0-9]+"); ;
                        hasLowerCaseLetter = Regex.IsMatch(newPassword, "[a-z]+"); ;
                        hasUpperCaseLetter = Regex.IsMatch(newPassword, "[A-Z]+");

                        if (hasDigit && hasLowerCaseLetter && hasUpperCaseLetter)
                        {
                            // accept 
                            acceptPassword = true;
                        }
                        
                    }

                } // end while (!acceptPassword)

                Console.WriteLine("newPassword: {0}", new String(newPasswordChar));
            }

            Console.WriteLine("[END]");
            Console.ReadLine();


        }
    }
}
